package com.dearbinge.data.service.impl.commodity;

import com.dearbinge.data.api.commodity.CommodityCommissionService;
import com.dearbinge.data.dao.CommodityMessageMapper;
import com.dearbinge.data.dao.commodity.CommodityCommissionMapper;
import com.dearbinge.data.pojo.commodity.CommodityCommissionDO;
import com.dearbinge.data.pojo.commodity.CommodityMessage;
import com.dearbinge.data.pojo.commodity.dto.CommodityCommissionResultDTO;
import com.dearbinge.data.pojo.commodity.vo.CommodityCommissionRequestVO;
import com.dearbinge.data.pojo.commodity.vo.CommodityCommissionResponseVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * 商品佣金实现层
 *
 * @author wes
 * @date 2019/3/11
 */
@Service("commodityCommissionService")
public class CommodityCommissionServiceImpl implements CommodityCommissionService {

    @Autowired
    private CommodityCommissionMapper commodityCommissionMapper;
    @Autowired
    private CommodityMessageMapper commodityMessageMapper;

    /**
     * 逻辑删除
     * @param commodityCommissionId 商品佣金表id
     * @return 结果
     */
    @Override
    public Integer delete(Integer commodityCommissionId) {
        return commodityCommissionMapper.deleteByPrimaryKey(commodityCommissionId);
    }

    /**
     * 通过id查询
     *
     * @param commodityCommissionId 商品佣金表id
     * @return 结果
     */
    @Override
    public CommodityCommissionResponseVO getById(Integer commodityCommissionId) {
        CommodityCommissionResponseVO commodityCommissionResponseVO = new CommodityCommissionResponseVO();
        CommodityCommissionDO commodityCommissionDO = commodityCommissionMapper.getById(commodityCommissionId);
        BeanUtils.copyProperties(commodityCommissionDO,commodityCommissionResponseVO);
        return commodityCommissionResponseVO;
    }

    /**
     * 插入
     *
     * @param commodityCommissionRequestVO 接受参数
     * @return 结果
     */
    @Override
    public Integer insert(CommodityCommissionRequestVO commodityCommissionRequestVO) {
        CommodityCommissionDO commodityCommissionDO = new CommodityCommissionDO();
        BeanUtils.copyProperties(commodityCommissionRequestVO,commodityCommissionDO);
        return commodityCommissionMapper.insert(commodityCommissionDO);
    }

    /**
     * 更新
     *
     * @param commodityCommissionRequestVO 接受参数
     * @return 结果
     */
    @Override
    public Integer update(CommodityCommissionRequestVO commodityCommissionRequestVO) {
        CommodityCommissionDO commodityCommissionDO = new CommodityCommissionDO();
        BeanUtils.copyProperties(commodityCommissionRequestVO,commodityCommissionDO);
        return commodityCommissionMapper.updateById(commodityCommissionDO);
    }


    /**
     * 根据规格id查询优惠多少元
     */
    @Override
    public CommodityCommissionResultDTO getDiscount(Integer commodityId){
        CommodityCommissionDO commodityCommissionDO = commodityCommissionMapper.getByCommodityId(commodityId);
        CommodityCommissionResultDTO commodityCommissionResultDTO = new CommodityCommissionResultDTO();
        if (commodityCommissionDO !=null) {
            //客户设置的批发价比例
            BigDecimal  wholesalePct = new BigDecimal(Double.toString(commodityCommissionDO.getWholesalePct()));
            //拿出来做活动的比例
            BigDecimal decimal = new BigDecimal(1);
            BigDecimal wholesale = decimal.subtract(wholesalePct);

            //优惠券比例
            BigDecimal couponPct =new BigDecimal(Double.toString( commodityCommissionDO.getCouponPct()));
            BigDecimal coupon = wholesale.multiply(couponPct);
            commodityCommissionResultDTO.setDiscountPct(coupon);

            BigDecimal forwardPct =new BigDecimal(Double.toString(commodityCommissionDO.getForwardPct()));
            BigDecimal forward = wholesale.multiply(forwardPct);
            commodityCommissionResultDTO.setCommissionPct(forward);

            BigDecimal showPct =new BigDecimal(Double.toString(commodityCommissionDO.getShowPct()));
            BigDecimal show = wholesale.multiply(showPct);
            commodityCommissionResultDTO.setSendShowPct(show);

            BigDecimal thumbsPct =new BigDecimal(Double.toString(commodityCommissionDO.getThumbsPct()));
            BigDecimal thumbs = wholesale.multiply(thumbsPct);
            commodityCommissionResultDTO.setRedEnvelopePct(thumbs);
            //封装展示价
            CommodityMessage commodityMessage = commodityMessageMapper.getCommodityById(commodityId);
            //优惠券金额
            commodityCommissionResultDTO.setDiscountPrice(commodityMessage.getRetailPrice().multiply(coupon));
            commodityCommissionResultDTO.setCommissionPrice(commodityMessage.getRetailPrice().multiply(forward));
        }
        return commodityCommissionResultDTO;
    }




}
